package my.algorithms;

import java.util.Stack;

/**
 * Created by IntelliJ IDEA.
 * User: jtruelove
 * Date: 3/23/11
 * Time: 3:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class MaxOverTime
{
    class Value
    {
        public Value(double value)
        {
            this.value = value;
            count = 1;
        }
        protected double value;
        protected int count;
    }

    private Stack<Value> maxes;

    private double max;

    public MaxOverTime()
    {
        maxes = new Stack<Value>();
    }

    public double getMax()
    {
        return max;
    }

    public void track(double newValue)
    {
        if(maxes.empty() ||  maxes.peek().value < newValue)
        {
            maxes.push(new Value(newValue));
            max = newValue;
        }
        else
        {
            maxes.peek().count++;
        }
    }

    public void remove()
    {
        if (maxes.peek().count == 1)
        {
            maxes.pop();
            max = maxes.peek().value;
        }
        else
        {
           maxes.peek().count--;
        }
    }

    public static void main (String [] args)
    {
        MaxOverTime theMax = new MaxOverTime();
        theMax.track(23);
        theMax.track(22);
        theMax.track(25);
        System.out.println("The max " + theMax.getMax());
        theMax.track(27);
        System.out.println("The max " + theMax.getMax());
        theMax.track(25);
        theMax.remove();
        theMax.remove();
        System.out.println("The max " + theMax.getMax());
        theMax.remove();
        System.out.println("The max " + theMax.getMax());
    }

}
